package com.anysoft.utils.image;

import android.graphics.Bitmap;
import android.text.TextUtils;
import android.widget.ImageView;

import com.anysoft.R;

/**
 * 自定义的BitmapUtils,实现三级缓存
 */
public class MyBitmapUtils {

    private NetCacheUtils mNetCacheUtils;
    private LocalCacheUtils mLocalCacheUtils;
    private MemoryCacheUtils mMemoryCacheUtils;

    public MyBitmapUtils() {
        mMemoryCacheUtils = new MemoryCacheUtils();
        mLocalCacheUtils = new LocalCacheUtils();
        mNetCacheUtils = new NetCacheUtils(mLocalCacheUtils, mMemoryCacheUtils);
    }

    public void disPlay(ImageView ivPic, String url) {
        //TODO 默认未加载图片，上线需确认
        ivPic.setImageResource(R.mipmap.banner_default);
        if (!TextUtils.isEmpty(url)) {
            Bitmap bitmap;
            //内存缓存
            bitmap = mMemoryCacheUtils.getBitmapFromMemory(url);
            if (bitmap != null) {
                ivPic.setImageBitmap(bitmap);
//                System.out.println("从内存获取图片啦.....");
                return;
            }

            //本地缓存
            bitmap = mLocalCacheUtils.getBitmapFromLocal(url);
            if (bitmap != null) {
                ivPic.setImageBitmap(bitmap);
//                System.out.println("从本地获取图片啦.....");
                //从本地获取图片后,保存至内存中
                mMemoryCacheUtils.setBitmapToMemory(url, bitmap);
                return;
            }
            //网络缓存
            mNetCacheUtils.getBitmapFromNet(ivPic, url);
        }
    }
}